package bhbt.utils;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;


public class Tools {
	
	
	/**
	 * 
	 * @param clazz
	 * @return
	 */
	public static String getWebPath(Class clazz ){
		String classPath = clazz.getProtectionDomain().getCodeSource().getLocation().getPath();
		String webPath = classPath.substring(0, classPath.indexOf("WEB-INF"));
		return webPath;
	}

	/**
	 * 得到上下文根路径
	 * @param request request
	 * @return String
	 */
	public static String getRootPath(HttpServletRequest request) {
		String rootPath = "";
		if(null == request){
			request = ContextUtil.getRequest();
		}
		if (null != request) {
			rootPath = "http://" + request.getServerName();
			int serverPort = request.getServerPort();
			if (0 < serverPort) {
				rootPath += ":" + serverPort;
			}
			rootPath += request.getContextPath()+"/";
		}
		return rootPath;
	}

}
